<template>
	<div id="approle">
		<form>
			<div class="search-container">
				<div class="search-input" style="float: left;">
					角色名称:<el-input placeholder="角色名称" v-model="PsRole.roleName" style="width: 200px;"></el-input>
				</div>
				<div class="search-input" style="float: left;">
					启用状态：
					<el-select placeholder="--请选择--" v-model="PsRole.isStart">
						<el-option label="启用" value="1"></el-option>
						<el-option label="禁用" value="0"></el-option>
					</el-select>
				</div>
				<div class="search-button" style="float: left;">
					<el-button @click.prevent="chaxun()" type="primary">查询</el-button>
				</div>

			</div>
		</form>
		<template>
			<el-table :data="zNodes" border style="width: 100%">
				<el-table-column prop="id" label="角色编号" width="180">
				</el-table-column>
				<el-table-column prop="rolename" label="角色名称" width="180">
				</el-table-column>
				<el-table-column prop="createdby" label="创建人">
				</el-table-column>
				<el-table-column prop="creationtime" label="更新时间">
				</el-table-column>
				<el-table-column prop="isstart" label="状态(启用/未启用)">
					<template #default="scope">
						<div>
							<el-radio v-model="scope.row.isstart" label="1" :disabled="true">通过</el-radio>
							<el-radio v-model="scope.row.isstart" label="0" :disabled="true">拒绝</el-radio>
						</div>
					</template>
				</el-table-column>

				<el-table-column>
					<template slot="header" slot-scope="scope">
						<el-button icon="el-icon-circle-plus-outline" type="primary" plain size="mini" slot="reference"
							@click="dialogFormVisibleAdd = true">添加角色</el-button>
					</template>
					<template slot-scope="scope">
						<el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>

						<el-button size="mini" type="danger"
							@click="handleDelete(scope.$index, scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination :page-size="3" :pager-count="10" style="margin-top: 20px;" background
				layout="prev, pager, next" :total="Usercount" @current-change="handleCurrentChange">
			</el-pagination>
		</template>

		<!-- 添加嵌套表单 -->
		<el-dialog title="添加角色" :visible.sync="dialogFormVisibleAdd" :append-to-body="true">
			<el-form :model="form">
				<el-form-item label="角色名称" >
					<el-input v-model="form.rolename" autocomplete="off" style="width: 500px;"></el-input>
				</el-form-item>
				<el-form-item label="状态(启用/未启用)">
					<template #default="scope">
						<div>
							<el-radio v-model="form.isstart" label="1">通过</el-radio>
							<el-radio v-model="form.isstart" label="0">拒绝</el-radio>
						</div>
					</template>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisibleAdd = false">取 消</el-button>
				<el-button type="primary" @click="childadd()">确 定</el-button>
			</div>
		</el-dialog>
		<!-- 添加嵌套表单 -->

		<!-- 修改嵌套表单 -->
		<el-dialog title="修改角色" :visible.sync="dialogFormVisibleupdate" :append-to-body="true">
			<el-form :model="form">
				<el-form-item label="角色编号" >
					<el-input v-model="form.id" autocomplete="off" style="width: 500px;" :disabled="true"></el-input>
				</el-form-item>
				<el-form-item label="角色名称" >
					<el-input v-model="form.rolename" autocomplete="off" style="width: 500px;"></el-input>
				</el-form-item>
				<el-form-item label="状态(启用/未启用)">
					<template #default="scope">
						<div>
							<el-radio v-model="form.isstart" label="1">通过</el-radio>
							<el-radio v-model="form.isstart" label="0">拒绝</el-radio>
						</div>
					</template>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisibleupdate = false">取 消</el-button>
				<el-button type="primary" @click="childUpdate()">确 定</el-button>
			</div>
		</el-dialog>
		<!-- 修改嵌套表单 -->



	</div>
</template>

<script>
	export default {
		data() {
			return {
				PsRole: {
					roleName: null,
					isStart: null
				},
				zNodes: [{}],
				Usercount:'',
				pager: "", //当前页
				//绑定资源表单显示
				dialogFormVisibleAdd: false,
				dialogFormVisibleupdate: false,
				form: {
					id: null,
					rolename: null,
					isstart: ''
				}
			}
		},
		methods: {
			// 查询方法
			chaxun() {
				this.axios.get("http://localhost:8080/ps-role/selectPage", {
					params: {
						rolename: this.PsRole.roleName,
						isstart: this.PsRole.isStart,
						nowPage: this.pager
					},
					headers: {}
				}).then((res) => {
					this.zNodes = res.data.datamap.fenye;
					this.Usercount=res.data.datamap.count;
				}).catch(err => {
					console.log(err)
				});
			},
			//添加方法
			childadd() {
				let that = this;
				console.log(JSON.stringify(that.form));
				this.axios.post("http://localhost:8080/ps-role/add", that.form).then((res) => {
					if (res.data == true) {
						alert("添加成功")
					}
					this.chaxun();
				}).catch(err => {
					console.log(err)
				})
				this.dialogFormVisibleAdd = false;
			},
			//修改给值方法
			handleEdit(index, row) {
				this.dialogFormVisibleupdate = true,
				this.form.id = row.id,
				this.form.rolename = row.rolename,
				this.form.isstart = row.isstart
			},
			childUpdate() {
				console.log(JSON.stringify(this.form));
				this.axios.post("http://localhost:8080/ps-role/update", this.form).then((res) => {
					if (res.data == true) {
						alert("修改成功")
					}
					this.chaxun();
				}).catch(err => {
					console.log(err)
				});
				this.dialogFormVisibleupdate = false;

			},
			//删除方法
			handleDelete(index, row) {
				console.log(index, row);
				this.$confirm('是否删除数据?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					let id = row.id;
					console.log(index, row.id);
					this.axios.get("http://localhost:8080/ps-role/delete/" + id, {
						params: {},
						headers: {}
					}).then((res) => {
						if (res.data == true) {
							alert("删除成功");
						}
						this.chaxun();
					}).catch(err => {
						console.log(err);
					});
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
			//分页查询
			handleCurrentChange(val) {
				this.pager = val;
				this.chaxun(val);
			}

		},
		mounted() {
			this.chaxun();
		}
	}
</script>

<style>
</style>